/**
 * Protocol for ATM machine server and client.
 * 
 * @author Alexander Gomez, Viktor Gavelli
 * 
 */
public class ATMProtocol {

	public ATMProtocol() {

	}

	public ATMProtocol(int start) {
		state = start;
	}

	/**
	 * Protocol states.
	 */
	private static final int USERNAME = 1;
	private static final int PASSWORD = 2;
	private static final int CHECK = 3;
	private static final int LOGGEDIN = 4;
	private static final int BALANCE = 5;
	private static final int WITHDRAWAL = 6;
	private static final int EXIT = 8;

	private int state = USERNAME; // Initialize state @ login - USERNAME.

	public int interpret(int input) {
		int retint = 4;

		if (state == USERNAME) {
			retint = input;
			state = PASSWORD;
		} else if (state == PASSWORD) {
			retint = input;
			state = CHECK;
		} else if (state == CHECK) {
			if (input == 0) {
				state = USERNAME;
			} else {
				state = LOGGEDIN;
			}
			retint = input;
		} else if (state == LOGGEDIN) {
			retint = input;
			if (input == 1) {
				state = BALANCE;
			} else if (input == 2) {
				state = WITHDRAWAL;
			} else if (input == 3) {
				state = WITHDRAWAL;
			} else if (input == 5) {
				state = EXIT;
			}
		} else if (state == BALANCE) {
			retint = input;
			state = LOGGEDIN;
		} else if (state == WITHDRAWAL) {
			retint = input;
			state = BALANCE;
		} else if (state == EXIT) {
			retint = 5;
		}

		return retint;
	}
}